package org.dairy.farms.nandana.web.controller;

import org.dairy.farms.model.Feed;
import org.dairy.farms.model.FeedType;
import org.dairy.farms.nandana.core.NandanaUtils;
import org.dairy.farms.nandana.core.logic.FeedManagementLogic;
import org.dairy.farms.nandana.web.view.FeedView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jul 12, 2011
 * Time: 4:31:13 PM
 */
@Controller
public class FeedController {
     private static final Logger logger = LoggerFactory.getLogger(FeedController.class);
    @Autowired
    FeedManagementLogic feedManagementLogic;
    @Autowired
    private NandanaUtils nandanaUtils;

    @RequestMapping(value="/feed/listFeedTypes",method = RequestMethod.GET)
    public void listFeedTypes(Model model){
        List<FeedType> feedTypesList = feedManagementLogic.getAllFeedTypes();
        model.addAttribute("feedTypesList",feedTypesList);
    }

    @RequestMapping(value="feed/feedDetails",method = RequestMethod.GET)
    public void feedDetails(@RequestParam(value="id",required = true)Long livestockId,Model model,
                              @RequestParam(value="page", required = true) int page,
                              @RequestParam(value="start", required = true) int start,
                              @RequestParam(value="limit", required = true) int limit){
        List<Feed> feedList = feedManagementLogic.feedDetails(livestockId,page,start,limit);
        long feedTotalCount = feedManagementLogic.feedDetailsTotalCount(livestockId);
        model.addAttribute("feedList",feedList);
        model.addAttribute("feedTotalCount",feedTotalCount);
    }

    @RequestMapping(value="feed/updateFeedDetails",method = RequestMethod.POST)
    public void updateFeedDetails(Model model, @RequestParam(value = "id", required = false) Long feedId,
                                  @RequestParam(value = "dateOfFeeding") String dateOfFeeding,
                                  @RequestParam(value = "livestockId") Long livestockId,
                                  @RequestParam(value = "timeOfFeeding") String timeOfFeeding,
                                  @RequestParam(value = "quantity") String quantity,
                                  @RequestParam(value = "feedTypeDescription") String feedTypeName,
                                  @RequestParam(value = "dateOfPayment") String dateOfPayment,
                                  @RequestParam(value = "cost") int cost,
                                  @RequestParam(value = "note",required = false) String note){
        FeedView feedView = new FeedView();
        feedView.setId(feedId);
        feedView.setDateOfFeedeing(nandanaUtils.mergeDateAndTime(dateOfFeeding,timeOfFeeding));
        feedView.setFeedTypeName(feedTypeName);
        feedView.setDateOfPayment(nandanaUtils.getDate(dateOfPayment));
        feedView.setCost(cost);
        feedView.setLivestockId(livestockId);
        feedView.setNotes(note);
        feedView.setQuantity(quantity);
        Feed feed = feedManagementLogic.updateFeedDetails(feedView);
        model.addAttribute("feed",feed);
    }

}
